package org.ws.edu.task3.ui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.ws.edu.task3.core.TestManager;
import org.ws.edu.task3.model.Answer;
import org.ws.edu.task3.model.Question;
import org.ws.edu.task3.model.Type;

public class ConsoleHandlerUI {
    private final TestManager manager = new TestManager();
    private static Logger logger = Logger.getLogger(ConsoleHandlerUI.class);

    public void handle() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        respondForQuestions();
        if (manager.hasPassQuestion()) {
            System.out.println("Do you want to respond pass questions?");
            if (br.readLine().trim().equalsIgnoreCase("yes"))
                respondForQuestions();
        }
        System.out.println(manager.getResult() + '\n');

    }

    public void typeInvalidTestNames() {
        String s[] = manager.getInvalidTest();
        for (String t : s) {
            System.out.println(t);
        }
    }

    public void chooseTestName() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            String[] s = manager.getValidTest();
            for (int i = 0; i < s.length; i++) {
                System.out.println(i + 1 + ". " + s[i]);
            }
            System.out.println("Enter the number of test...");
            int indexOfTest = Integer.parseInt(br.readLine().trim()) - 1;
            manager.setNameTest(s[indexOfTest]);

        } catch (NumberFormatException e) {
            logger.debug("NumberFormatException", e);
        }
    }

    private void respondForQuestions() throws IOException {
        while (manager.hasMoreQuestion()) {

            manager.setAnswer(getUserAnswer(manager.getNextQuestion()));
        }
    }

    private List<Answer> getUserAnswer(Question q) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        List<Answer> answers = q.getAllAnswers();
        List<Answer> userAnswers = new ArrayList<Answer>();
        try {
            System.out.println(q.getName());
            for (int i = 0; i < answers.size(); i++) {
                System.out.println(i + 1 + ". " + answers.get(i).getName());
            }
            System.out.println("If Pass question enter \"pass\"");
            System.out.println("enter the answer(-s) ...");
            String answer = br.readLine();
            if (answer.equalsIgnoreCase("pass")) {
                return userAnswers;
            }
            if (manager.getType().equals(Type.SCALES)) {
                userAnswers.add(answers.get(Integer.parseInt(answer.trim()) - 1));
                return userAnswers;
            }
            String[] answersIndex = answer.split(",");
            for (String s : answersIndex) {
                userAnswers.add(answers.get(Integer.parseInt(s.trim()) - 1));
            }

        } catch (NumberFormatException e) {
            System.out.println("Incorrect format of answer \nRetry give answer on question");
            userAnswers = getUserAnswer(q);
            logger.warn("Incorrect format of answer", e);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Incorrect № of answer \nRetry give answer on question");
            logger.info("Incorrect number of answer");
            userAnswers = getUserAnswer(q);
        }

        return userAnswers;

    }

}